package MyBlog.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import MyBlog.biz.ArticleBiz;
import MyBlog.biz.CommentBiz;
import MyBlog.entity.Article;
import MyBlog.entity.Comment;
import MyBlog.entity.User;

import com.opensymphony.xwork2.ActionSupport;


@Scope("prototype")
@Component("commentAction")
public class CommentAction extends ActionSupport {

public int getArticleId() {
		return articleId;
	}

	//	参数
	private int articleId;
	private Comment comment;
	private Article article;
	private User commentUser;
	public Article getArticle() {
		return article;
	}
	public void setComment(Comment comment) {
		this.comment = comment;
	}
	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}
	
//	数组
	private List<Comment> comments;
	public List<Comment> getComments() {
		return comments;
	}
	
//	实现类
	@Autowired
	private CommentBiz commentBiz;
	@Autowired
	private ArticleBiz articleBiz;
	
//	方法
	public String getCommentList(){
		commentUser = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		article = articleBiz.fetchArticleById(articleId);
		article.setClicks(article.getClicks()+1);
		comments = commentBiz.getCommentsByArticleId(articleId);
		return SUCCESS;
	}
	
	public String add(){
		article = articleBiz.fetchArticleById(articleId);
		commentUser = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		comment.setArticle(article);
		System.out.println(article.getUser().getId());
		comment.setUser(commentUser);
		commentBiz.add(comment);
		article.setCommentTimes(article.getCommentTimes()+1);
		return SUCCESS;
	}
}
